package de.luh.sim.ftn.views;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import de.luh.sim.ftn.panel.DrawPanel;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JSpinner;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;
import java.text.NumberFormat;

import javax.swing.JTextField;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;



/**
 * Main GUI
 * 
 * @author Dominik Molitor
 * @version 0.7
 * 
 */
public class mainView {

	private JFrame frmFaulttolerantnetworks;

	JComboBox<String> comboBox;
	DrawPanel drawPanel;
	JSpinner numberOfNodes;
	private JTextField textTimeField;
	private JTextField textFaultTolerance;
	private JTextField textDFaultTolerance;
	private JTextField textSpanningRatio;
	private JTextField textAvgDistance;
	private JTextField textTotalLength;
	private JTextField textCost;
	private JTextField textPerformance;
	private JTextField textEfficiency;
	NumberFormat nf;

	private JTextField textEdges;
	private JSlider sliderCostToFtRatio;
	private JTextField textCostChange;
	private JTextField textFtChange;
	private JLabel lblCostToFtRatio;
	private JSlider sliderMS;
	private JLabel lblMS;

	/**
	 * Create the application.
	 */
	public mainView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		frmFaulttolerantnetworks = new JFrame();
		frmFaulttolerantnetworks.setTitle("FaultTolerantNetworks");
		frmFaulttolerantnetworks.setBounds(100, 100, 958, 587);
		frmFaulttolerantnetworks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmFaulttolerantnetworks.getContentPane().setLayout(null);

		JPanel menuPanel = new JPanel();
		menuPanel.setBounds(10, 11, 150, 500);
		frmFaulttolerantnetworks.getContentPane().add(menuPanel);
		menuPanel.setLayout(null);

		JButton btnGenerateMst = new JButton("Use Algorithm");
		btnGenerateMst.setBounds(10, 110, 130, 23);
		menuPanel.add(btnGenerateMst);

		JButton btnNewNodesButton = new JButton("New Nodes");

		btnNewNodesButton.setBounds(10, 11, 130, 23);
		menuPanel.add(btnNewNodesButton);

		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] { "MST PrimsAlgorithm", "All Edges", "Hill Climbing",
				"Differential Evolution" }));
		comboBox.setBounds(10, 76, 130, 23);
		menuPanel.add(comboBox);

		numberOfNodes = new JSpinner();
		numberOfNodes.setModel(new SpinnerNumberModel(new Integer(10), new Integer(2), null, new Integer(1)));
		numberOfNodes.setBounds(10, 45, 43, 20);
		menuPanel.add(numberOfNodes);

		sliderCostToFtRatio = new JSlider();
		sliderCostToFtRatio.setMaximum(100);
		sliderCostToFtRatio.setMinimum(0);
		sliderCostToFtRatio.setBounds(0, 210, 143, 45);
		sliderCostToFtRatio.setMajorTickSpacing(25);
		sliderCostToFtRatio.setPaintTicks(true);
		sliderCostToFtRatio.setPaintLabels(true);
		menuPanel.add(sliderCostToFtRatio);

		JLabel lblCostToFault = new JLabel("Fault Tolerance : Cost");
		lblCostToFault.setBounds(10, 194, 133, 14);
		menuPanel.add(lblCostToFault);

		lblCostToFtRatio = new JLabel("0.50");
		lblCostToFtRatio.setBounds(13, 266, 46, 14);
		menuPanel.add(lblCostToFtRatio);

		JLabel lblWantedCost = new JLabel("Cost:");
		lblWantedCost.setBounds(10, 144, 80, 14);
		menuPanel.add(lblWantedCost);

		JLabel lblWantedFaultTolerance = new JLabel("Fault Tolerance:");
		lblWantedFaultTolerance.setBounds(10, 169, 102, 14);
		menuPanel.add(lblWantedFaultTolerance);

		textCostChange = new JTextField();
		textCostChange.setText("2.0");
		textCostChange.setBounds(107, 141, 43, 20);
		menuPanel.add(textCostChange);
		textCostChange.setColumns(10);

		textFtChange = new JTextField();
		textFtChange.setText("0.8");
		textFtChange.setColumns(10);
		textFtChange.setBounds(107, 166, 43, 20);
		menuPanel.add(textFtChange);

		sliderMS = new JSlider();
		sliderMS.setValue(0);
		sliderMS.setPaintTicks(true);
		sliderMS.setPaintLabels(true);
		sliderMS.setMinimum(0);
		sliderMS.setMaximum(1000);
		sliderMS.setMajorTickSpacing(250);
		sliderMS.setBounds(0, 422, 140, 45);
		menuPanel.add(sliderMS);

		lblMS = new JLabel("0");
		lblMS.setBounds(7, 475, 46, 14);
		menuPanel.add(lblMS);
		
		JLabel lblDelayForPainting = new JLabel("Delay for painting in MS");
		lblDelayForPainting.setBounds(7, 401, 133, 14);
		menuPanel.add(lblDelayForPainting);

		drawPanel = new DrawPanel();
		drawPanel.setBounds(170, 11, 500, 500);
		frmFaulttolerantnetworks.getContentPane().add(drawPanel);

		JPanel panel = new JPanel();
		panel.setBounds(680, 11, 252, 500);
		frmFaulttolerantnetworks.getContentPane().add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("Time: ");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(10, 11, 120, 14);
		panel.add(lblNewLabel);

		JLabel lblFaultTolerance = new JLabel("Fault Tolerance:");
		lblFaultTolerance.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblFaultTolerance.setBounds(10, 36, 120, 14);
		panel.add(lblFaultTolerance);

		JLabel lblSpanningRatio = new JLabel("Spanning Ratio: ");
		lblSpanningRatio.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblSpanningRatio.setBounds(10, 86, 120, 14);
		panel.add(lblSpanningRatio);

		JLabel lblAverageDistance = new JLabel("Average Distance: ");
		lblAverageDistance.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblAverageDistance.setBounds(10, 142, 120, 14);
		panel.add(lblAverageDistance);

		JLabel lblTotalLength = new JLabel("Total Length: ");
		lblTotalLength.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblTotalLength.setBounds(10, 167, 120, 14);
		panel.add(lblTotalLength);

		JLabel lblCost = new JLabel("Cost: ");
		lblCost.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblCost.setBounds(10, 192, 120, 14);
		panel.add(lblCost);

		JLabel lblPerformance = new JLabel("Performance: ");
		lblPerformance.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblPerformance.setBounds(10, 217, 120, 14);
		panel.add(lblPerformance);

		JLabel lblEfficiency = new JLabel("Efficiency: ");
		lblEfficiency.setBackground(Color.GREEN);
		lblEfficiency.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblEfficiency.setBounds(10, 242, 120, 14);
		panel.add(lblEfficiency);

		JLabel lblDoubleFaultTolerance = new JLabel("Double Fault Tolerance:");
		lblDoubleFaultTolerance.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblDoubleFaultTolerance.setBounds(10, 61, 120, 14);
		panel.add(lblDoubleFaultTolerance);

		textTimeField = new JTextField();
		textTimeField.setBounds(130, 8, 112, 20);
		panel.add(textTimeField);
		textTimeField.setColumns(10);

		textFaultTolerance = new JTextField();
		textFaultTolerance.setBounds(130, 33, 112, 20);
		panel.add(textFaultTolerance);
		textFaultTolerance.setColumns(10);

		textDFaultTolerance = new JTextField();
		textDFaultTolerance.setBounds(130, 58, 112, 20);
		panel.add(textDFaultTolerance);
		textDFaultTolerance.setColumns(10);

		textSpanningRatio = new JTextField();
		textSpanningRatio.setBounds(130, 83, 112, 20);
		panel.add(textSpanningRatio);
		textSpanningRatio.setColumns(10);

		textAvgDistance = new JTextField();
		textAvgDistance.setBounds(130, 139, 112, 20);
		panel.add(textAvgDistance);
		textAvgDistance.setColumns(10);

		textTotalLength = new JTextField();
		textTotalLength.setBounds(130, 164, 112, 20);
		panel.add(textTotalLength);
		textTotalLength.setColumns(10);

		textCost = new JTextField();
		textCost.setBounds(130, 189, 112, 20);
		panel.add(textCost);
		textCost.setColumns(10);

		textPerformance = new JTextField();
		textPerformance.setBounds(130, 214, 112, 20);
		panel.add(textPerformance);
		textPerformance.setColumns(10);

		textEfficiency = new JTextField();
		textEfficiency.setBounds(130, 239, 112, 20);
		panel.add(textEfficiency);
		textEfficiency.setColumns(10);

		JLabel lblNumberOfEdges = new JLabel("Number of Edges");
		lblNumberOfEdges.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNumberOfEdges.setBounds(10, 114, 120, 14);
		panel.add(lblNumberOfEdges);

		textEdges = new JTextField();
		textEdges.setColumns(10);
		textEdges.setBounds(130, 111, 112, 20);
		panel.add(textEdges);

		JMenuBar menuBar = new JMenuBar();
		frmFaulttolerantnetworks.setJMenuBar(menuBar);

		JMenu mnPlaceholder = new JMenu("Placeholder");
		menuBar.add(mnPlaceholder);

		JMenuItem mntmPlaceholder = new JMenuItem("Placeholder");
		mnPlaceholder.add(mntmPlaceholder);

		JMenuItem mntmPlaceholder_1 = new JMenuItem("Placeholder");
		mnPlaceholder.add(mntmPlaceholder_1);

		sliderCostToFtRatio.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				double costToFtRatio = ((double) sliderCostToFtRatio.getValue()) / 100.0;
				drawPanel.setCostToFtRatio(costToFtRatio);
				lblCostToFtRatio.setText(Double.toString(costToFtRatio));

			}
		});

		sliderMS.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				lblMS.setText(Integer.toString(sliderMS.getValue()));

			}
		});

		btnNewNodesButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				drawPanel.createPoints((int) numberOfNodes.getValue());
				textTotalLength.setText("");
				textCost.setText("");
				textAvgDistance.setText("");
				textEdges.setText("");
				textFaultTolerance.setText("");
				drawPanel.repaint();
			}
		});

		btnGenerateMst.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedItem().equals("MST PrimsAlgorithm")) {
					System.out.println("Doing MST PrimsAlgo");
					drawPanel.setMSTPrimsAlgo();
					System.out.println("DONE");

				} else if (comboBox.getSelectedItem().equals("All Edges")) {
					System.out.println("Doing ALL EDGES");
					drawPanel.setAllEdges();
					System.out.println("DONE");
				} else if (comboBox.getSelectedItem().equals("Hill Climbing")) {
					System.out.println("Doing Hill Climbing");
					drawPanel.setWantedCost(Double.parseDouble(textCostChange.getText()));
					drawPanel.setWantedFt(Double.parseDouble(textFtChange.getText()));
					drawPanel.hillClimbing(sliderMS.getValue());
					System.out.println("DONE");
				}
				textTotalLength.setText(nf.format(drawPanel.getTotalLength()));
				textCost.setText(nf.format(drawPanel.cost()));
				textAvgDistance.setText(nf.format(drawPanel.averageDistance()));
				textEdges.setText(Integer.toString(drawPanel.numberOfEdges()));
				textFaultTolerance.setText(nf.format(drawPanel.faultTolerance()));
				textEfficiency.setText(nf.format(drawPanel.getCurrentEfficieny()));
				drawPanel.repaint();
			}
		});
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainView window = new mainView();
					window.frmFaulttolerantnetworks.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
